package com.xiaodeng.xdblogspringboot.controller;

import com.xiaodeng.xdblogspringboot.interceptors.AdminInterceptor;
import com.xiaodeng.xdblogspringboot.pojo.Category;
import com.xiaodeng.xdblogspringboot.pojo.Result;
import com.xiaodeng.xdblogspringboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加类别
     *
     * @param category 类别
     * @return 结果
     */
    @AdminInterceptor.AdminIntercept
    @PostMapping("addCategory")
    public Result addCategory(@RequestBody Category category){
        categoryService.addcategory(category);
        return Result.success();
    }

    /**
     * 获取所有分类
     * @return 返回分类列表
     */
    @GetMapping("getCategory")
    public Result<List<Category>> getCategory(){
        List<Category> list=categoryService.getCategory();
        return Result.success(list);
    }

    /**
     * @param id id
     * @return 信息
     */
    // 删除文章分类
    @AdminInterceptor.AdminIntercept
    //用户注销
    @DeleteMapping("/delete")
    public Result delete(Integer id){
        categoryService.delete(id);
        return Result.success("删除分类成功");
    }

    // TODO 获取与特定类别关联的所有文章。

    /**
     * @param category category
     * @return category
     *///  更新类别
    @AdminInterceptor.AdminIntercept
    @PutMapping("update")
    public Result<Category> update(@RequestBody Category category){
        categoryService.update(category);
        return Result.success(category);
    }
}
